﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Mail;
using System.Web;
using UniversityStudentManagemnetSystem.DAO.DAL;
using UniversityStudentManagemnetSystem.DAO.DAL.Gateway;

namespace UniversityStudentManagemnetSystem.BLL
{
    public class StudentRegistrationManager
    {
        protected enum Students
        {
            deptMaxStudent = 25,
            uniqueKeyLength = 4
        }

        private string studentRegNo;
        StudentRegistationGateway gateway=new StudentRegistationGateway();
        public void SaveEducationType(Student student)
        {
            gateway.SaveStudentRegistationProcess(student);
        }


        public string Save(Student aStudent)
        {
            DateTime time = aStudent.RegistrationDate;
            string regDate = time.Year.ToString();
            int previousStudentCount=gateway.PreviousStudentCount(aStudent.Department.DeptId);
           
            string message;
            if (aStudent.Department.DeptId == 0)
            {
                message = "Department is Missing";
                return message;
            }
            else if (aStudent.Name == "")
            {
                message = "Name Is Missing";
                return message;
            }
            else if (aStudent.Email == "")
            {
                message = "Please Fill up the Email Field Properly";
                return message;
            }
            else if (aStudent.ContactNumber == "")
            {
                message = "Contact No is Missing";
                return message;
            }
            else if (!CheakEmail(aStudent.Email))
            {
                message = "Sorry Your Email Address is not Valid!Please Try Again!";
                return message;
            }
            else if (gateway.HasThisEmail(aStudent))
            {

             return message = "This Email Already Exist!Try Another!";

            }
            
            else  if (previousStudentCount == (int)Students.deptMaxStudent)
            {
                return message= string.Format("This year the depatrment already contains {0} students", (int)Students.deptMaxStudent);
            }
            else
           
            {

                int studentSerialNo = ++previousStudentCount;
                string uniqueNumber = studentSerialNo.ToString().PadLeft((int)Students.uniqueKeyLength, '0');

                aStudent.Department.Code = gateway.GetCodefor(aStudent.Department.DeptId);

                aStudent.RegNo = regDate + aStudent.Department.Code + uniqueNumber;

                if (gateway.SaveStudentRegistationProcess(aStudent))
                {
                    message = (" Registation Successfull.\nName : " + aStudent.Name + "\nEmail :  " + aStudent.Email + "\nRegistation No : " + aStudent.RegNo);
                 return message;
                }
                

            }
            studentRegNo = aStudent.RegNo;
            return message="Unknown Error";

        }

        private bool CheakEmail(string email)
        {
            try
            {

                MailAddress studentEmail=new MailAddress(email);
                return true;
            }
            catch (FormatException)
            {

                return false;
            }
        }

        internal string AStudentEducationInformationSave(StudentEducationRegistration education, Student studenteducation, out string message)
        {
             int previousStudentCount = gateway.PreviousStudentCount(studenteducation.Department.DeptId);
             if (studenteducation.Email=="")
             {
                 message = "Email is Missing";
                 throw new Exception(message);
             }

             else if (gateway.HasThisEmail(studenteducation))
             {

              message = "This Email Already Exist!Try Another!";
                 throw new Exception(message);
              
             }
             else if (previousStudentCount == (int)Students.deptMaxStudent)
             {
                 return message = string.Format("This year the depatrment already contains {0} students", (int)Students.deptMaxStudent);
             }
             else 
             {
                 gateway.SaveStudentEducationInformation(studenteducation, education,studentRegNo);
                 message = "Student Education Information Save"; 
             }
            return message;

        }

        public DataTable GetCurrentStudentAllCertificate(Student aStudent)
        {
          return gateway.GetCurrentStudentAllCertificate(aStudent);
        }
    }
}